/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：实现配置静态常量的编辑器窗口
* CreateTime：2020-08-01 22:48:01
* Version：1.0
* Modify Recorder：
*************************************************/

using UnityEngine;
using UnityEditor;
using System.IO;
using TinyToolKit;
using TinyToolKit.UI;
using UnityEditorInternal;

namespace TinyToolKitEditor
{
    public class DrawWindowConstTable : DrawWindowBase
    {
        private string ScriptContent =
                "public class #name#\r\n"
              + "{\r\n"
              + "//#DefineContent#"
              + "}";

        private SerializedObject _serializedObject;
        private ReorderableList uiFormType;
        private ReorderableList objectPoolType;
        private ReorderableList fsmType;
        private ReorderableList customType;
        
        private TinyToolEditorSettingAsset _toolSetting; 
         
        public override void InitWindow(ScriptableObject setting)
        {
            _toolSetting = (TinyToolEditorSettingAsset) setting;
            _serializedObject = new SerializedObject(_toolSetting);

            uiFormType = TinyToolWindowUtil.GetReorderableList(_serializedObject, "UIFormType", "窗体","常量");
            objectPoolType = TinyToolWindowUtil.GetReorderableList(_serializedObject, "ObjectPoolType", "对象池","常量");
            fsmType = TinyToolWindowUtil.GetReorderableList(_serializedObject, "FsmType", "有限状态机","常量");
            customType = TinyToolWindowUtil.GetReorderableList(_serializedObject, "CustomType", "自定义","常量");
        }

        protected override void DrawInternalMainGUI()
        {
            _serializedObject.Update();
            
            uiFormType.DoLayoutList();
            objectPoolType.DoLayoutList();
            fsmType.DoLayoutList();
            customType.DoLayoutList();

            _toolSetting.constTableName = TinyToolWindowUtil.DrawTextField("常量类名：", _toolSetting.constTableName);
            EditorGUILayout.BeginHorizontal();
            _toolSetting.constTableSavePath = EditorGUILayout.TextField("存储路径：", _toolSetting.constTableSavePath);
            if (GUILayout.Button(EditorGUIUtility.FindTexture("Folder Icon"), GUILayout.MaxWidth(40), GUILayout.MaxHeight(20)))
            {
                string savePath = EditorUtility.OpenFolderPanel("常量类存储路径", "Assets", "Scripts");
                if (savePath.IsNullOrEmpty() == false)
                {
                    int index = savePath.IndexOf("Assets");
                    savePath = savePath.Remove(0, index);
                    _toolSetting.constTableSavePath = savePath;
                } 
            }
            EditorGUILayout.EndHorizontal();

            if (TinyToolWindowUtil.DrawButton("创建常量类"))
            {
                if (_toolSetting.constTableSavePath.IsNullOrEmpty() == false)
                {
                    if (Directory.Exists(_toolSetting.constTableSavePath) == false)
                        TinyToolWindowUtil.DrawWarnningDialog("存储路径不是有效路径！");
                    else
                        CreateStaticClass(_toolSetting.constTableSavePath);
                }
                else
                {
                    TinyToolWindowUtil.DrawWarnningDialog("存储路径不能为空！");
                }
            }
            
            _serializedObject.ApplyModifiedProperties();
        }

        /// <summary>
        /// 创建静态类
        /// </summary>
        /// <param name="savePath">保存的路径</param>
        private void CreateStaticClass(string savePath)
        {
            string str = string.Empty;
            string targetPath = Path.Combine(savePath, _toolSetting.constTableName + ".cs");
            if (File.Exists(targetPath)) File.Delete(targetPath);
            File.Create(targetPath).Dispose();

            if (_toolSetting.UIFormType.Length > 0)
            {
                for (int i = 0; i < _toolSetting.UIFormType.Length; i++)
                {
                    str += string.Format("    public const string UI_{0} = \"{0}\";\t\n", _toolSetting.UIFormType[i]); 
                }
            }

            if (_toolSetting.ObjectPoolType.Length > 0)
            {
                str += "\n";
                for (int i = 0; i < _toolSetting.ObjectPoolType.Length; i++)
                {
                    str += string.Format("    public const string ObjectPool_{0} = \"{0}\";\t\n", _toolSetting.ObjectPoolType[i]);
                }
            }

            if (_toolSetting.FsmType.Length > 0)
            {
                str += "\n";
                for (int i = 0; i < _toolSetting.FsmType.Length; i++)
                {
                    str += string.Format("    public const string FSM_{0} = \"{0}\";\t\n", _toolSetting.FsmType[i]);
                }
            }

            if (_toolSetting.CustomType.Length > 0)
            {
                str += "\n";
                for (int i = 0; i < _toolSetting.CustomType.Length; i++)
                {
                    str += string.Format("    public const string Custom_{0} = \"{0}\";\t\n", _toolSetting.CustomType[i]);
                }
            }

            ScriptContent = ScriptContent.Replace("#name#", _toolSetting.constTableName);
            ScriptContent = ScriptContent.Replace("//#DefineContent#", str);
            File.WriteAllText(targetPath, ScriptContent);

            EditorUtility.DisplayDialog("创建自定义常量", _toolSetting.constTableName + ".cs 常量保存在" + savePath, "确定");
        }
    }
}

